package com.wlyuan.open.scanner.application.handler.table;

import com.alibaba.fastjson.JSON;
import com.wlyuan.open.scanner.application.annotation.TableScannerName;
import com.wlyuan.open.scanner.constant.ScannerCodeEnum;
import com.wlyuan.open.scanner.constant.TableEnum;
import com.wlyuan.open.scanner.repository.WayBillRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Service
@RequiredArgsConstructor
@TableScannerName(isDefault = false, tableName = TableEnum.ORDER_EXTENSION)
public class ExtensionTableToIncrHandler extends BaseTableToIncrHandler {
    private final WayBillRepository wayBillRepository;
    private static final String TYPE = "Extension_type";

    @Override
    public List<String> handler(String tableName, String data, String beforeData) {

        var businessParams = params(data, beforeData);

        if (StringUtils.isBlank(businessParams)) {
            logger.error("{}表业务分流参数不能为空", tableName);
            return Collections.emptyList();
        }
        return Arrays.asList(ScannerCodeEnum.TOP_EXTENSION.getName());
    }

    private String params(String data, String beforeData) {
        var map = JSON.parseObject(data, Map.class);
        //businessParams 为 srcClass
        var businessParams = String.valueOf(map.get(TYPE));
        logger.info("TMS_Extension type params : {}", businessParams);
        var type = Integer.valueOf(businessParams);
        if (type == 0) {
            return businessParams;
        }
        return "";
    }

}
